package xyz.haijin.spider;

import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;
import xyz.haijin.spider.dao.WallpaperDetailDao;
import xyz.haijin.spider.dao.WallpaperTypeDao;
import xyz.haijin.spider.entity.WallpaperDetail;
import xyz.haijin.spider.entity.WallpaperType;
import xyz.haijin.zt.mysql.core.BaseDAO;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author liuhaijin
 * @Date 2022/3/29
 */
public class EnterDeskDetailRepoPageProcessor implements PageProcessor {

    /**
     * 日志对象
     */
    private Logger logger  = LoggerFactory.getLogger(EnterDeskDetailRepoPageProcessor.class);


    /**
     * 壁纸类型数据库对象
     */
    private BaseDAO<WallpaperType> wallpaperTypeDao = new WallpaperTypeDao();

    /**
     * 壁纸详情数据库对象
     */
    private BaseDAO<WallpaperDetail> wallpaperDetailDao = new WallpaperDetailDao();

    /**
     * 壁纸详情数据
     */
    private WallpaperDetail wallpaperDetail;

    public void setWallpaperDetail(WallpaperDetail wallpaperDetail) {
        this.wallpaperDetail = wallpaperDetail;
    }

    private static final String URL = "https://m.sj.enterdesk.com";

    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(10000)
            //添加cookie之前一定要先设置主机地址，否则cookie信息不生效
            .setDomain("m.sj.enterdesk.com")
            //添加抓包获取的cookie信息
            .addCookie("r", "25")
            .addCookie("t", "0bfad35d7af9b409dcb9f21541df42de");


    @SneakyThrows
    @Override
    public void process(Page page) {
        Html html = page.getHtml();
        Selectable detail = html.xpath("/html/body/div[16]/div[2]");
        Selectable dataHref = detail.xpath("//div/@data-href");
        System.out.println("dataHref:"+dataHref);
        String url = page.getUrl().toString();
//        System.out.println("html:"+html);
        System.out.println("当前的url："+url);
        WallpaperDetail wallpaperDetailUpdate = WallpaperDetail.builder()
                .wallpaperDetailUrl(dataHref.toString())
                .build();
        wallpaperDetailDao.update(wallpaperDetailUpdate,wallpaperDetail.getId());
    }

    @Override
    public Site getSite() {
        return site;
    }


    /**
     * 获取壁纸类型请求url
     * @return
     */
    public List<String> listWallpaperTypeUrl () {
        List<WallpaperType> wallpaperTypeList = wallpaperTypeDao.findAll();
        List<String> list = new ArrayList<>();
        for (WallpaperType wallpaperType : wallpaperTypeList) {
            if ("手机壁纸".equals(wallpaperType.getTypeName())) {
                list.add(wallpaperType.getTypeUrl()+"/");
            } else {
                list.add(URL+wallpaperType.getTypeUrl());
            }
        }
        return list;
    }

    /**
     * 获取壁纸的详情数据
     * @return
     */
    public List<WallpaperDetail> listWallpaperDetail() throws SQLException {
        return wallpaperDetailDao.findWhereSql(1);
    }

    public static void main(String[] args) throws SQLException {
        EnterDeskDetailRepoPageProcessor enterDeskDetailRepoPageProcessor = new EnterDeskDetailRepoPageProcessor();
        List<WallpaperDetail> list = enterDeskDetailRepoPageProcessor.listWallpaperDetail();
        for (int i = 0; i < list.size(); i++) {
            EnterDeskDetailRepoPageProcessor enter = new EnterDeskDetailRepoPageProcessor();
            enter.setWallpaperDetail(list.get(i));
            Spider.create(enter).addUrl(list.get(i).getWallpaperDetailClickUrl()).thread(5).run();
        }
    }
}
